﻿using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using System.Web; 

namespace Sx.Utils
{
    public static class AuthHelper
    {
        public static string CookieName = ".rxsec";
        //private static JSONParameters Parameter;
        //static AuthHelper() {
        //    Parameter = new JSONParameters { EnableAnonymousTypes = true, UseFastGuid = true, SerializeNullValues = false, DateTimeMilliseconds = true, UseUTCDateTime = false };
        //}
        public static async Task SignInAsync(string userid, object userData)
        {
            var data = JsonHelper.ToJson(userData);


            var context = SxHttpContext.Current;
            if (context == null)
                throw new InvalidOperationException();
            var claims = new List<Claim> {
                new Claim("userid", userid), 
                new Claim("userData", data)
            };

            var claimsIdentity = new ClaimsIdentity(
                claims,
                CookieAuthenticationDefaults.AuthenticationScheme);

            await context.SignInAsync(
                CookieAuthenticationDefaults.AuthenticationScheme,
                new ClaimsPrincipal(claimsIdentity),
                new AuthenticationProperties
                {
                    IsPersistent = true,
                    ExpiresUtc = DateTime.UtcNow.AddMinutes(20)
                });
        }

        public static async Task SingOutAsync()
        {
            await SxHttpContext.Current.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
            //FormsAuthentication.SignOut();
        }
  
        public static T GetUserData<T>() where T : class, new()
        {
            T UserData = null;
            try
            {
                var context = SxHttpContext.Current;
                if (!context.User.Identity.IsAuthenticated)
                {
                    return null;
                }
                var userData = context.User.Claims.Where(c => c.Type == "userData").ToList();
                if (userData != null && userData.Count>0)
                {
                    UserData = JsonHelper.ToObject<T>(userData[0].Value);
                }  
            }
            catch
            { }

            return UserData;
        }

        /// <summary>
        /// 是否登录
        /// </summary>
        /// <returns></returns>
        public static bool IsAuth() {
            var context = SxHttpContext.Current;
            if (context.User.Identity.IsAuthenticated)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static string GetUserid() {
            string name = "";
            try
            {
                var context = SxHttpContext.Current;
                if (!context.User.Identity.IsAuthenticated)
                {
                    return null;
                }
                var userData = context.User.Claims.Where(c => c.Type == "userid").ToList();
                if (userData != null && userData.Count > 0)
                {
                    name = userData[0].Value;
                }
            }
            catch
            { } 
            return name; 
        }
    }
}
